home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 413 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: "Paul D. DeRocco" <pderocco@ix.netcom.com>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Exceptions and Destructors
  5. Date: 20 Feb 1996 10:36:27 PST
  6. Organization: Netcom
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <3125B9C0.7241@ix.netcom.com>
  9. References: <BGLENDEN.96Feb14173755@colobus.aoc.nrao.edu>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: Sat, 17 Feb 1996 06:19:28 -0500
  12. X-Netcom-Date: Sat Feb 17  3:25:55 AM PST 1996
  13. X-Mailer: Mozilla 2.0b6a (Win95; I)
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBVAwUBMSoUvEy4NqrwXLNJAQFi/QH5ARVYgQC5UnZzVWcgEjzL2cBJNkgBx4JN
  16.     WfqSWl82lUx2JuyTgeDUHRGnuAGy95OF1nU7EYIK60ZV1BeK1jGyzA==
  17.     =5PNg
  18. Originator: austern@isolde.mti.sgi.com
  19.  
  20. Although the language doesn't mandate this, it's a good idea to obey the 
  21. rule: never, _never_, NEVER throw an exception from within a destructor. 
  22. If there's one operation that should be guaranteed to "work", it's 
  23. destroying an object. Remember that destructors are automatically called 
  24. during stack unwinding when an exception is thrown. If such a destructor 
  25. should throw another exception, what should happen? In fact, the 
  26. standard states that in this situation, terminate() will be called, and 
  27. terminate() is only one notch less violent than abort().
  28.  
  29. You can, of course, use exceptions _within_ a destructor, as long as you 
  30. handle them all. Just don't let them leak out, or you'll be hosed.
  31.  
  32. If an error might occur during destruction of an object, try to design 
  33. in a separate termination function that can be called first. For 
  34. instance, say you've got an object that represents an open file, and 
  35. destroying the object is supposed to commit any buffers and close the 
  36. file. What happens if a disk write occurs during the buffer writing?
  37.  
  38. The best approach is to design the destructor so that the error is 
  39. swallowed, to avoid violating the above rule. If you need to see the 
  40. error, supply a commit() member that can be called before destroying the 
  41. object, that will detect any disk errors and leave the file object in a 
  42. state that can be destroyed without causing any errors.
  43.  
  44. -- 
  45.  
  46. Ciao,
  47. Paul D. DeRocco
  48. ---
  49. [ To submit articles: Try just posting with your newsreader.  If that fails,
  50.                       use mailto:std-c++@ncar.ucar.edu
  51.   FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  52.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  53.   Comments? mailto:std.c++-request@ncar.ucar.edu 
  54. ]
  55.